/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ez2find.backing;

import com.ez2find.manager.AreaManagerLocal;
import com.ez2find.manager.JobCategorieManagerLocal;
import com.ez2find.manager.JobManagerLocal;
import com.ez2find.manager.LocationManagerLocal;
import com.ez2find.manager.RegisterUserManagerLocal;
import com.ez2find.model.Area;
import com.ez2find.model.Job;
import com.ez2find.model.CategoriaTrabajo;
import com.ez2find.model.Location;
import com.ez2find.model.RegisterUser;
import com.ez2find.services.EmailLocal;
import com.ez2find.util.RequestContext;
import com.ez2find.util.SessionContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrador
 */
public class JobBean {

    @EJB
    private JobManagerLocal ejbJobManager;
    @EJB
    private JobCategorieManagerLocal ejbJobCategorieManager;
    @EJB
    private RegisterUserManagerLocal ejbUserManager;
    @EJB
    private EmailLocal ejbEmail;
    @EJB
    private AreaManagerLocal ejbAreaManager;
    @EJB
    private LocationManagerLocal ejbLocationManager;
    private Job job;
    private int back;
    private long jobCategorieId;
    private long areaId;
    private String email1;
    private List<String> categories;
    private boolean linkRPOk;
    private boolean createOk;
    private boolean payOk;
    private boolean maskedEmail;
    private int page;
    private String searchValue;
    private boolean searchTelecommuting;
    private boolean searchPartTime;
    private boolean searchContract;
    private boolean searchNonProfit;
    private boolean searchIntership;
    private boolean onlySearchTitles;
    private boolean hasImage;

    public Job getJob() {
        if (job == null) {
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
            HttpSession session = request.getSession();
            if (session.isNew()) {
                try {
                    String errorPageURL = externalContext.getRequestContextPath() + "/faces/welcome.jsp";
                    externalContext.redirect(errorPageURL);
                    FacesMessage msg = new FacesMessage("The session has bean expired");
                    msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    return null;
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                    return null;
                }
            } else {
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                if (sessionBean.getEjbSession().getJob() != null) {
                    job = sessionBean.getEjbSession().getJob();
                    if(job.getTypeReplyTo().equals("masked email"))
                        maskedEmail=true;
                    areaId = 0;
                    if (job.getArea() != null) {
                        areaId = job.getArea().getId();
                    }
                    categories = new ArrayList<String>();
                    if (job.getFee() == null) {
                        jobCategorieId = job.getJobCategories().get(0).getId();
                    } else {
                        for (CategoriaTrabajo jobCategorie : job.getJobCategories()) {
                            categories.add(String.valueOf(jobCategorie.getId()));
                        }
                    }
                } else {
                    job = new Job();
                    job.setTypeReplyTo("masked email");
                    if (!sessionBean.getAreasCurrentSite().isEmpty()) {
                        if (sessionBean.getEjbSession().getCurrentArea() != null) {
                            areaId = sessionBean.getEjbSession().getCurrentArea().getId();
                        } else {
                            areaId = sessionBean.getEjbSession().getCurrentSite().getAreas().get(1).getId();
                        }
                    }
                    categories = new ArrayList<String>();
                    try {
                        PostBean postBean = (PostBean) RequestContext.getAttribute("postBean");
                        back = postBean.getBack();
                    } catch (Exception ex) {
                    }
                }
            }
        }
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    public List<Job> getlistAll() {
        return ejbJobManager.listAll();
    }

    public String create() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/jobPosting/create.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            try {
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                if (sessionBean.getEjbSession().getOwner() != null) {
                    job.setCode((ejbJobManager.lastCode()) + 1);
                    job.setType("job");
                    job.setUser(sessionBean.getEjbSession().getOwner());
                    job.setEmail(sessionBean.getEjbSession().getOwner().getEmail());
                    if(job.getTypeReplyTo().equals("masked email"))
                        ejbEmail.addAnonymousEmail("job", job.getCode(), job.getEmail());
                    job.setPublishDate(new Date(System.currentTimeMillis()));
                    job.setStatus("active");
                    job.setCreateDate(new Date(System.currentTimeMillis()));
                    job.setSite(sessionBean.getEjbSession().getCurrentSite());
                    if (areaId != 0) {
                        job.setArea(ejbAreaManager.find(areaId));
                    }
                    if (categories.isEmpty()) {
                        CategoriaTrabajo jobCategorie = ejbJobCategorieManager.find(jobCategorieId);
                        job.getJobCategories().add(jobCategorie);
                        job.setCategorie(jobCategorie.getCategorie());
                    } else {
                        int i = 0;
                        for (String categorie : categories) {
                            CategoriaTrabajo jobCategorie = ejbJobCategorieManager.find(Integer.parseInt(categorie));
                            job.getJobCategories().add(jobCategorie);
                            if (i == 0) {
                                job.setCategorie(jobCategorie.getCategorie());
                            } else {
                                job.setCategorie(job.getCategorie() + ", " + jobCategorie.getCategorie());
                            }
                            i++;
                        }
                        job.setNumCategories(categories.size());
                    }
                    ejbJobManager.create(job);
                    FacesMessage msg = new FacesMessage("Your post has been successfully registered.");
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    FacesMessage msg1 = new FacesMessage("To manage your post,  log into your account.");
                    FacesContext.getCurrentInstance().addMessage(null, msg1);
                    createOk = true;
                    payOk = false;
                    return "success";
                } else {
                    if ((job.getEmail().length() < 4) || !job.getEmail().contains("@") || !job.getEmail().contains(".") || (email1.length() < 4) || !email1.contains("@") || !email1.contains(".") || !job.getEmail().equals(email1)) {
                        FacesMessage msg = new FacesMessage("The emails address don't equals or invalids");
                        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                        FacesContext.getCurrentInstance().addMessage(null, msg);
                        return null;
                    }
                    RegisterUser userAccount = ejbUserManager.findUserByEmail(job.getEmail());
                    if (userAccount != null) {
                        if (userAccount.getStatus().equals("active") || userAccount.getStatus().equals("reactive")) {
                            job.setUser(userAccount);
                            job.setCode((ejbJobManager.lastCode()) + 1);
                            job.setType("job");
                            job.setStatus("pending");
                            job.setCreateDate(new Date(System.currentTimeMillis()));
                            job.setSite(sessionBean.getEjbSession().getCurrentSite());
                            if (areaId != 0) {
                                job.setArea(ejbAreaManager.find(areaId));
                            }
                            if (categories.isEmpty()) {
                                CategoriaTrabajo jobCategorie = ejbJobCategorieManager.find(jobCategorieId);
                                job.getJobCategories().add(jobCategorie);
                                job.setCategorie(jobCategorie.getCategorie());
                            } else {
                                int i = 0;
                                for (String categorie : categories) {
                                    CategoriaTrabajo jobCategorie = ejbJobCategorieManager.find(Integer.parseInt(categorie));
                                    job.getJobCategories().add(jobCategorie);
                                    if (i == 0) {
                                        job.setCategorie(jobCategorie.getCategorie());
                                    } else {
                                        job.setCategorie(job.getCategorie() + ", " + jobCategorie.getCategorie());
                                    }
                                    i++;
                                }
                                job.setNumCategories(categories.size());
                            }
                            ejbJobManager.create(job);
                            FacesMessage msg = new FacesMessage("Your post has been successfully registered.");
                            FacesContext.getCurrentInstance().addMessage(null, msg);
                            FacesMessage msg1 = new FacesMessage("The User Account from this email " + userAccount.getEmail() + " exist.");
                            FacesContext.getCurrentInstance().addMessage(null, msg1);
                            FacesMessage msg2 = new FacesMessage("If you forgot your password or need to reactive your account clik in the link to recovery password.");
                            FacesContext.getCurrentInstance().addMessage(null, msg2);
                            FacesMessage msg3 = new FacesMessage("Please enter in your account and published your posting.");
                            FacesContext.getCurrentInstance().addMessage(null, msg3);
                            linkRPOk = true;
                            createOk = true;
                            payOk = false;
                            return "success";
                        }
                        //userAccount.setDefaultSite(sessionBean.getEjbSession().getCurrentSite());
                        userAccount.setRegisterDate(new Date(System.currentTimeMillis()));
                        Random r = new Random();
                        String token = Long.toString(Math.abs(r.nextLong()), 36);
                        userAccount.setConfirmationCode(token);
                        ejbUserManager.update(userAccount, false);
                        ejbEmail.sendAccountConfirmation(userAccount);
                        job.setUser(userAccount);
                    } else {
                        RegisterUser registerUser = new RegisterUser();
                        registerUser.setEmail(job.getEmail());
                        //registerUser.setDefaultSite(sessionBean.getEjbSession().getCurrentSite());
                        registerUser.setNumberOfPostings(50);
                        registerUser.setRegisterDate(new Date(System.currentTimeMillis()));
                        registerUser.setStatus("pending");
                        Random r = new Random();
                        String token = Long.toString(Math.abs(r.nextLong()), 36);
                        registerUser.setConfirmationCode(token);
                        ejbUserManager.create(registerUser);
                        RegisterUser newRegisterUser = ejbUserManager.merge(registerUser);
                        ejbEmail.sendAccountConfirmation(newRegisterUser);
                        job.setUser(newRegisterUser);
                    }
                    job.setCode((ejbJobManager.lastCode()) + 1);
                    job.setType("job");
                    job.setStatus("pending");
                    job.setCreateDate(new Date(System.currentTimeMillis()));
                    job.setSite(sessionBean.getEjbSession().getCurrentSite());
                    if (areaId != 0) {
                        job.setArea(ejbAreaManager.find(areaId));
                    }
                    if (categories.isEmpty()) {
                        CategoriaTrabajo jobCategorie = ejbJobCategorieManager.find(jobCategorieId);
                        job.getJobCategories().add(jobCategorie);
                        job.setCategorie(jobCategorie.getCategorie());
                    } else {
                        int i = 0;
                        for (String categorie : categories) {
                            CategoriaTrabajo jobCategorie = ejbJobCategorieManager.find(Integer.parseInt(categorie));
                            job.getJobCategories().add(jobCategorie);
                            if (i == 0) {
                                job.setCategorie(jobCategorie.getCategorie());
                            } else {
                                job.setCategorie(job.getCategorie() + ", " + jobCategorie.getCategorie());
                            }
                            i++;
                        }
                        job.setNumCategories(categories.size());
                    }
                    ejbJobManager.create(job);
                    FacesMessage msg = new FacesMessage("Your post has been successfully registered.");
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    FacesMessage msg1 = new FacesMessage("For Administrate your Post eez2find creates an Account for you using the email address " + job.getEmail() + " to reply this post.");
                    FacesContext.getCurrentInstance().addMessage(null, msg1);
                    FacesMessage msg2 = new FacesMessage("Please active your account from email received and published your posting.");
                    FacesContext.getCurrentInstance().addMessage(null, msg2);
                    createOk = true;
                    payOk = false;
                    return "success";
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                FacesMessage msg = new FacesMessage("Error your Post can't register, maybe the email could not be sent or it doesn't exist");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
        }
    }

    public String previewCreate() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/jobPosting/create.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            if (sessionBean.getEjbSession().getOwner() != null) {
                job.setEmail(sessionBean.getEjbSession().getOwner().getEmail());
                return "success";
            } else {
                if ((job.getEmail().length() < 4) || !job.getEmail().contains("@") || !job.getEmail().contains(".") || (email1.length() < 4) || !email1.contains("@") || !email1.contains(".") || !job.getEmail().equals(email1)) {
                    FacesMessage msg = new FacesMessage("The emails address don't equals or invalids");
                    msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    return null;
                }
                return "success";
            }
        }
    }

    public String pay() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/jobPosting/create.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            if (sessionBean.getEjbSession().getOwner() != null) {
                job.setFee(String.valueOf(sessionBean.getEjbSession().getCurrentSite().getJobPrice() * categories.size()) + " $");
                createOk = true;
                payOk = true;
                return "success";
            } else {
                if ((job.getEmail().length() < 4) || !job.getEmail().contains("@") || !job.getEmail().contains(".") || (email1.length() < 4) || !email1.contains("@") || !email1.contains(".") || !job.getEmail().equals(email1)) {
                    FacesMessage msg = new FacesMessage("The emails address don't equals or invalids");
                    msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    return null;
                }
                job.setFee(String.valueOf(sessionBean.getEjbSession().getCurrentSite().getJobPrice() * categories.size()) + " $");
                createOk = true;
                payOk = true;
                return "success";
            }
        }
    }

    public String update() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/welcome.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            try {
                if (job.getFee() != null && categories.size() > job.getNumCategories()) {
                    FacesMessage msg = new FacesMessage("The number of categories dont more at " + job.getNumCategories());
                    msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    return null;
                }
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                if(job.getTypeReplyTo().equals("masked email") && !maskedEmail)
                    ejbEmail.addAnonymousEmail("job", job.getCode(), job.getEmail());
                if(!job.getTypeReplyTo().equals("masked email") && maskedEmail)
                    ejbEmail.deleteAnonymousEmail("job", job.getCode(), job.getEmail());
                if (areaId != 0) {
                    job.setArea(ejbAreaManager.find(areaId));
                } else {
                    job.setArea(null);
                }
                job.getJobCategories().clear();
                if (categories.isEmpty()) {
                    CategoriaTrabajo jobCategorie = ejbJobCategorieManager.find(jobCategorieId);
                    job.getJobCategories().add(jobCategorie);
                    job.setCategorie(jobCategorie.getCategorie());
                } else {
                    int i = 0;
                    for (String categorie : categories) {
                        CategoriaTrabajo jobCategorie = ejbJobCategorieManager.find(Integer.parseInt(categorie));
                        job.getJobCategories().add(jobCategorie);
                        if (i == 0) {
                            job.setCategorie(jobCategorie.getCategorie());
                        } else {
                            job.setCategorie(job.getCategorie() + ", " + jobCategorie.getCategorie());
                        }
                        i++;
                    }
                }
                ejbJobManager.update(job);
                FacesMessage msg = new FacesMessage("Successful edit your Post");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                sessionBean.getEjbSession().setJob(null);
                SessionContext.removeAttribute("jobBean");
                return "success";
            } catch (Exception ex) {
                ex.printStackTrace();
                FacesMessage msg = new FacesMessage("Error your Post can't edit");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
        }
    }

    public String previewUpdate() {
        if (job.getFee() != null && categories.size() > job.getNumCategories()) {
            FacesMessage msg = new FacesMessage("The number of categories dont more at " + job.getNumCategories());
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
        return "success";
    }

    public List<String> getCategories() {
        return categories;
    }

    public void setCategories(List<String> categories) {
        this.categories = categories;
    }

    public int getCategoriesSize() {
        return categories.size();
    }

    public long getAreaId() {
        return areaId;
    }

    public void setAreaId(long areaId) {
        this.areaId = areaId;
    }

    public Area getArea() {
        return ejbAreaManager.find(areaId);
    }

    public List<Location> getLocationsJobArea() {
        try {
            return ejbLocationManager.locationsArea(ejbAreaManager.find(areaId));
        } catch (Exception ex) {
            return new ArrayList<Location>();
        }
    }

    public boolean isCreateOk() {
        return createOk;
    }

    public void setCreateOk(boolean createOk) {
        this.createOk = createOk;
    }

    public boolean isLinkRPOk() {
        return linkRPOk;
    }

    public void setLinkRPOk(boolean linkRPOk) {
        this.linkRPOk = linkRPOk;
    }

    public String homeUser() {
        SessionContext.removeAttribute("jobBean");
        return "homeUser";
    }

    public int getBack() {
        return back;
    }

    public void setBack(int back) {
        this.back = back;
    }

    public long getJobCategorieId() {
        if (jobCategorieId == 0) {
            try {
                PostBean postBean = (PostBean) RequestContext.getAttribute("postBean");
                jobCategorieId = postBean.getCategorieId();
            } catch (Exception ex) {
            }
        }
        return jobCategorieId;
    }

    public void setJobCategorieId(long jobCategorieId) {
        this.jobCategorieId = jobCategorieId;
    }

    public List<Job> getListJobs() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/jobPosting/list.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            String consult = "";
            boolean jobC = false;
            if (jobCategorieId > 0) {
                consult = consult + " AND j.jobCategories=:jobCategorie";
                jobC = true;
            }
            if (searchTelecommuting) {
                consult = consult + " AND j.telecommuting=1";
            }
            if (searchPartTime) {
                consult = consult + " AND j.partTime=1";
            }
            if (searchContract) {
                consult = consult + " AND j.contract=1";
            }
            if (searchNonProfit) {
                consult = consult + " AND j.nonProfit=1";
            }
            if (searchIntership) {
                consult = consult + " AND j.intership=1";
            }
            if (hasImage) {
                consult = consult + " AND j.description like '%<img src=%'";
            }
            if (searchValue != null && !searchValue.equals("")) {
                if (onlySearchTitles) {
                    consult = consult + " AND j.title like '%" + searchValue + "%'";
                } else {
                    consult = consult + " AND (j.title like '%" + searchValue + "%' OR j.description like '%" + searchValue + "%')";
                }
            }
            SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            if (sessionBean.getEjbSession().getCurrentArea() == null) {
                return ejbJobManager.listJobsSite(sessionBean.getEjbSession().getCurrentSite(), jobC, ejbJobCategorieManager.find(jobCategorieId), consult);
            } else {
                return ejbJobManager.listJobsArea(sessionBean.getEjbSession().getCurrentArea(), jobC, ejbJobCategorieManager.find(jobCategorieId), consult);
            }
        }
    }

    public String backView() {
        job = null;
        return "backJob" + back;
    }

    public String backCreate() {
        SessionContext.removeAttribute("jobBean");
        return "back" + back;
    }

    public String backEdit() {
        SessionContext.removeAttribute("jobBean");
        return "back";
    }

    public String getEmail1() {
        return email1;
    }

    public void setEmail1(String email1) {
        this.email1 = email1;
    }

    public boolean isPayOk() {
        return payOk;
    }

    public void setPayOk(boolean payOk) {
        this.payOk = payOk;
    }

    public Date getToDay() {
        return new Date(System.currentTimeMillis());
    }

    public String getDescription() {
        try {
            if (job.getDescription().contains("\n")) {
                return job.getDescription().replaceAll("\n", "<br/>");
            } else {
                return job.getDescription().replaceAll("\r", "<br/>");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return "Do not Message";
        }
    }

    public String getSearchValue() {
        if(searchValue==null){
            try {
                PostBean postBean = (PostBean) RequestContext.getAttribute("postBean");
                searchValue = postBean.getSearchValue();
            } catch (Exception ex) {
            }
        }
        return searchValue;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public boolean isSearchTelecommuting() {
        return searchTelecommuting;
    }

    public void setSearchTelecommuting(boolean searchTelecommuting) {
        this.searchTelecommuting = searchTelecommuting;
    }

    public boolean isSearchPartTime() {
        return searchPartTime;
    }

    public void setSearchPartTime(boolean searchPartTime) {
        this.searchPartTime = searchPartTime;
    }

    public boolean isSearchContract() {
        return searchContract;
    }

    public void setSearchContract(boolean searchContract) {
        this.searchContract = searchContract;
    }

    public boolean isSearchNonProfit() {
        return searchNonProfit;
    }

    public void setSearchNonProfit(boolean searchNonProfit) {
        this.searchNonProfit = searchNonProfit;
    }

    public boolean isSearchIntership() {
        return searchIntership;
    }

    public void setSearchIntership(boolean searchIntership) {
        this.searchIntership = searchIntership;
    }

    public boolean isOnlySearchTitles() {
        return onlySearchTitles;
    }

    public void setOnlySearchTitles(boolean onlySearchTitles) {
        this.onlySearchTitles = onlySearchTitles;
    }

    public boolean isHasImage() {
        return hasImage;
    }

    public void setHasImage(boolean hasImage) {
        this.hasImage = hasImage;
    }

    public int getPage() {
        if (page == 0) {
            page = 1;
        }
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }
    
    public int getContador(){
        try{
            return 20000-job.getDescription().length();
        }catch(Exception ex){
            return 20000;
        }
    }
    
    public void setContador(int contador){}
    

    public void flaggingMiscategorized(){
        try {
            job.setMiscategorized(ejbJobManager.merge(job).getMiscategorized()+1);
            ejbJobManager.update(job);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void flaggingProhibited(){
        try {
            job.setProhibited(ejbJobManager.merge(job).getProhibited()+1);
            ejbJobManager.update(job);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void flaggingSpam(){
        try {
            job.setSpam(ejbJobManager.merge(job).getSpam()+1);
            ejbJobManager.update(job);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void flaggingBest(){
        try {
            job.setBest(ejbJobManager.merge(job).getBest()+1);
            ejbJobManager.update(job);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
